from django.db.models.signals import post_save
from django.dispatch import receiver
from django.db import models
from django.contrib.auth.models import User

# Create your models here.


class UserExtension(models.Model):
    user = models.OneToOneField(
        User, related_name='extension', on_delete=models.CASCADE)
    telephone = models.CharField(max_length=100)
    school = models.CharField(max_length=100)

    class Meta:
        permissions = (
            ('view_userextension1', '查看用户的权限'),
        )


@receiver(post_save, sender=User)
def handler_user_extension(sender, instance, created, **kwargs):
    if created:
        UserExtension.objects.create(user=instance)
    else:
        instance.extension.save()


class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = '文章列表'
        permissions = (('auditarticle', '审核文章'), ('publisharticle', '发布文章'))
